package sjtu.ist.warehouse4ws.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.json.annotations.JSON;


import sjtu.ist.warehouse4ws.model.CustDomainmodel;
import sjtu.ist.warehouse4ws.model.CustEpcunits;
import sjtu.ist.warehouse4ws.model.CustRole;
import sjtu.ist.warehouse4ws.model.Domainmodel;
import sjtu.ist.warehouse4ws.model.Epcunits;
import sjtu.ist.warehouse4ws.model.Functionpoint;
import sjtu.ist.warehouse4ws.model.Role;

import sjtu.ist.warehouse4ws.service.RoleService;
import sjtu.ist.warehouse4ws.service.cust.CustRoleService;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ShowRoleListAction extends ActionSupport{
	private RoleService roleService;
	private CustRoleService custRoleService;
	private List<Role> roleList = new ArrayList<Role>();
	private String gid;
	private String vid;
	
	//获得角色列表
	public String showRoleList(){	
		Subject currentUser = SecurityUtils.getSubject();
		Integer company=(Integer) currentUser.getSession().getAttribute("companyId");
		
		//ActionContext actionContext=ActionContext.getContext();
		//Map session=actionContext.getSession();		
		//int userType=(Integer)(session.get("user_type"));
		//String userCompany=session.get("user_company").toString();	
		
		if(!currentUser.hasRole("superadmin")){
			this.roleList=new ArrayList<Role>();
			List<CustRole> templist=custRoleService.getCustRoleList();
			for(CustRole r : templist){
			roleList.add(r);
			}
			/*
			List<CustRole> templist=custRoleService.getCustRoleListByCompanyId(userCompany);
			this.roleList = new ArrayList<Role>();
			for(CustRole r : templist){
				//if(privilegeService.checkPrivilege(d.getFileId(),userCompany,userType, "read")){
					roleList.add(r);
				//}
			}*/
		}
		else{
			//roleList = roleService.getRoleList();
			//System.out.println("gid:"+gid);
			//System.out.println("vid:"+vid);
			roleList = roleService.getRoleListByGroupAndVersion(gid,vid);
		}		
		return SUCCESS;
	}
	
	@JSON(name = "totalRole")
	public int getTotalRole() {
		return this.roleList.size();
	}

	@JSON(name = "Role")
	public List<Role> getRoleList() {
		return this.roleList;
	}
	
	// 注入DepartmentService
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	
	public void setCustRoleService(CustRoleService custRoleService) {
		this.custRoleService = custRoleService;
	}

	public String getGid() {
		return gid;
	}

	public void setGid(String gid) {
		this.gid = gid;
	}

	public String getVid() {
		return vid;
	}

	public void setVid(String vid) {
		this.vid = vid;
	}

}
